package com.iot.nev.gateway.connector.hbase;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import com.iot.nev.gateway.utils.UtilTools;

public class SaveDataForHBase {

	// 数据库元数据操作对象
	private static Admin admin;
	// 与HBase数据库的连接对象
	private static Connection connection;
	private static Properties  hbaseProperties = UtilTools.getConfig(System.getProperty("user.dir") + "/configuration/config.properties");
	public static int num = 0;
	
	public static void saveData(String message) throws IOException{
    	int a1[] = {3, 5, 1, 5466, 9999, 2645, 6355, 12, 13, 14, 42345, 15, 16};
    	int a2[] = {2, 12, 3, 210, 32345, 24567, 187, 45678, 284955, 2, 227, 54789, 24578, 168, 54678, 192};
    	int a3[] = {7000, 3112, 32132, 22442, 151, 11, 10450, 23, 11223, 52, 52};
    	int a4[] = {16, 100, 3800};
    	int a5[] = {5, 5, 5};
    	int a6[] = {44, 45, 89, 58, 59, 151, 26, 20, 97, 19, 4, 15};
    	int a7[] = {2, 65535, 7, 7, 7, 7};
    	int a8[] = {1 ,1 ,8888, 88, 21, 54, 78};
    	int a9[] = {1, 221, 55871};
    	// 设置环境变量
    	// System.setProperty("hadoop.home.dir", "/usr/hdp/2.6.1.0-129/hadoop");
    	// System.setProperty("hadoop.home.dir", "E:\\hadoop-common-2.2.0-bin-master");
    	if(connection==null || admin==null){
			String server = hbaseProperties.getProperty("hbase_server_ip");
			String port = hbaseProperties.getProperty("hbase_server_port");
			String path = hbaseProperties.getProperty("hbase_parent_path");
			// 取得一个数据库连接的配置参数对象
			Configuration conf = HBaseConfiguration.create();
			// 设置连接参数：HBase数据库所在的主机IP
			conf.set("hbase.zookeeper.quorum", server);
			// 设置连接参数：HBase数据库使用的端口
			conf.set("hbase.zookeeper.property.clientPort", port);
			conf.set("zookeeper.znode.parent", path);
			// 取得一个数据库连接对象
			try {
				connection = ConnectionFactory.createConnection(conf);
				admin = connection.getAdmin();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}		
		// 取得一个数据表对象
    	String hbaseName = hbaseProperties.get("hbase_table_name").toString();
		Table table = connection.getTable(TableName.valueOf(hbaseName));
		// 需要插入数据库的数据集合
		List<Put> putList = new ArrayList<Put>();
		Put put;
        String tm = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss:SSS").format(new Date());
        double x = Math.random();
        String t = tm + "," + x;
        
		put = new Put(Bytes.toBytes(t));
		byte[] b_0x01 = Bytes.toBytes("0x01");
		byte[] b_0x02 = Bytes.toBytes("0x02");
		byte[] b_0x03 = Bytes.toBytes("0x03");
		byte[] b_0x04 = Bytes.toBytes("0x04");
		byte[] b_0x05 = Bytes.toBytes("0x05");
		byte[] b_0x06 = Bytes.toBytes("0x06");
		byte[] b_0x07 = Bytes.toBytes("0x07");
		byte[] b_0x08 = Bytes.toBytes("0x08");
		byte[] b_0x09 = Bytes.toBytes("0x09");
		put.addColumn(b_0x01, Bytes.toBytes("bVehicle_Status1"),Bytes.toBytes(a1[0]));
		put.addColumn(b_0x01, Bytes.toBytes("bCharge_Status1"),Bytes.toBytes(a1[1]));
		put.addColumn(b_0x01, Bytes.toBytes("bRunning_Mode1"),Bytes.toBytes(a1[2]));
		put.addColumn(b_0x01, Bytes.toBytes("wVehicle_Speed1"),Bytes.toBytes(a1[3]));
		put.addColumn(b_0x01, Bytes.toBytes("wTotal_Voltage1"),Bytes.toBytes(a1[4]));
		put.addColumn(b_0x01, Bytes.toBytes("wTotal_Current1"),Bytes.toBytes(a1[5]));
		put.addColumn(b_0x01, Bytes.toBytes("bSOC1"),Bytes.toBytes(a1[6]));
		put.addColumn(b_0x01, Bytes.toBytes("bDCDC_Status1"),Bytes.toBytes(a1[7]));
		put.addColumn(b_0x01, Bytes.toBytes("bGear1"),Bytes.toBytes(a1[8]));
		put.addColumn(b_0x01, Bytes.toBytes("wResPositive1"),Bytes.toBytes(a1[9]));
		put.addColumn(b_0x01, Bytes.toBytes("bAccPedal1"),Bytes.toBytes(a1[10]));
		put.addColumn(b_0x01, Bytes.toBytes("bBrkPedal1"),Bytes.toBytes(a1[11]));
		
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Num2"),Bytes.toBytes(a2[0]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_NO2"),Bytes.toBytes(a2[1]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Status2"),Bytes.toBytes(a2[2]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Controller_Temperature2"),Bytes.toBytes(a2[3]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Speed2"),Bytes.toBytes(a2[4]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Torque2"),Bytes.toBytes(a2[5]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Temperature2"),Bytes.toBytes(a2[6]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Voltage2"),Bytes.toBytes(a2[7]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Current2"),Bytes.toBytes(a2[8]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_NO22"),Bytes.toBytes(a2[9]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Status22"),Bytes.toBytes(a2[10]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Controller_Temperature22"),Bytes.toBytes(a2[11]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Speed22"),Bytes.toBytes(a2[12]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Torque22"),Bytes.toBytes(a2[13]));
		put.addColumn(b_0x02, Bytes.toBytes("bMotor_Temperature22"),Bytes.toBytes(a2[14]));
		put.addColumn(b_0x02, Bytes.toBytes("wMotor_Voltage22"),Bytes.toBytes(a2[15]));

		put.addColumn(b_0x03, Bytes.toBytes("wBattery_Voltage3"),Bytes.toBytes(a3[0]));
		put.addColumn(b_0x03, Bytes.toBytes("wBattery_Current3"),Bytes.toBytes(a3[1]));
		put.addColumn(b_0x03, Bytes.toBytes("wFuel_Cnsumption_Rate3"),Bytes.toBytes(a3[2]));
		put.addColumn(b_0x03, Bytes.toBytes("wTemperature_Probes_Num3"),Bytes.toBytes(a3[3]));
		put.addColumn(b_0x03, Bytes.toBytes("wMax_Temperature3"),Bytes.toBytes(a3[4]));
		put.addColumn(b_0x03, Bytes.toBytes("bMax_Temperature_Probe_No3"),Bytes.toBytes(a3[5]));
		put.addColumn(b_0x03, Bytes.toBytes("wMax_Hydrogen_Concentration3"),Bytes.toBytes(a3[6]));
		put.addColumn(b_0x03, Bytes.toBytes("bMax_Hydrogen_Concentration_No3"),Bytes.toBytes(a3[7]));
		put.addColumn(b_0x03, Bytes.toBytes("wMax_Hydrogen_Pressure3"),Bytes.toBytes(a3[8]));
		put.addColumn(b_0x03, Bytes.toBytes("bMax_Hydrogen_Pressure_No3"),Bytes.toBytes(a3[9]));
		put.addColumn(b_0x03, Bytes.toBytes("bHigh_Voltage_DCDC_STT3"),Bytes.toBytes(a3[10]));
		
		put.addColumn(b_0x04, Bytes.toBytes("bEngine_Status4"),Bytes.toBytes(a4[0]));
		put.addColumn(b_0x04, Bytes.toBytes("wCrankshaft_Speed4"),Bytes.toBytes(a4[1]));
		put.addColumn(b_0x04, Bytes.toBytes("wFuel_Consumption_Rate4"),Bytes.toBytes(a4[2]));
		
		put.addColumn(b_0x05, Bytes.toBytes("bGPS_Status5"),Bytes.toBytes(a5[0]));
		put.addColumn(b_0x05, Bytes.toBytes("dLatitude5"),Bytes.toBytes(a5[1]));
		put.addColumn(b_0x05, Bytes.toBytes("dLongitude5"),Bytes.toBytes(a5[2]));

		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Subsystem_Max_Voltage_No6"),Bytes.toBytes(a6[0]));
		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Cell_Max_Voltage_No6"),Bytes.toBytes(a6[1]));
		put.addColumn(b_0x06, Bytes.toBytes("wBattery_Cell_Max_Voltage6"),Bytes.toBytes(a6[2]));
		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Subsystem_Min_Voltage_No6"),Bytes.toBytes(a6[3]));
		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Cell_Min_Voltage_No6"),Bytes.toBytes(a6[4]));
		put.addColumn(b_0x06, Bytes.toBytes("wBattery_Cell_Min_Voltage6"),Bytes.toBytes(a6[5]));
		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Subsystem_Max_T_No6"),Bytes.toBytes(a6[6]));
		put.addColumn(b_0x06, Bytes.toBytes("bProbe_Max_T_No6"),Bytes.toBytes(a6[7]));
		put.addColumn(b_0x06, Bytes.toBytes("wProbe_Max_T6"),Bytes.toBytes(a6[8]));
		put.addColumn(b_0x06, Bytes.toBytes("bBattery_Subsystem_Min_T_No6"),Bytes.toBytes(a6[9]));
		put.addColumn(b_0x06, Bytes.toBytes("bProbe_Min_T_No6"),Bytes.toBytes(a6[10]));
		put.addColumn(b_0x06, Bytes.toBytes("wProbe_Min_T6"),Bytes.toBytes(a6[11]));

		put.addColumn(b_0x07, Bytes.toBytes("bHighest_Alarm_Level7"),Bytes.toBytes(a7[0]));
		put.addColumn(b_0x07, Bytes.toBytes("dGeneral_Alarm_Flag7"),Bytes.toBytes(a7[1]));
		put.addColumn(b_0x07, Bytes.toBytes("bBattery_Error_Num7"),Bytes.toBytes(a7[2]));
		put.addColumn(b_0x07, Bytes.toBytes("bMotor_Error_Num7"),Bytes.toBytes(a7[3]));
		put.addColumn(b_0x07, Bytes.toBytes("bEngine_Error_Num7"),Bytes.toBytes(a7[4]));
		put.addColumn(b_0x07, Bytes.toBytes("bOther_Error_Num7"),Bytes.toBytes(a7[5]));

		put.addColumn(b_0x08, Bytes.toBytes("bEnergyStorageSubsystemsNum8"),Bytes.toBytes(a8[0]));
		put.addColumn(b_0x08, Bytes.toBytes("bEnergyStorageSubsystemsNo8"),Bytes.toBytes(a8[1]));
		put.addColumn(b_0x08, Bytes.toBytes("wVoltage8"),Bytes.toBytes(a8[2]));
		put.addColumn(b_0x08, Bytes.toBytes("wCurrent8"),Bytes.toBytes(a8[3]));
		put.addColumn(b_0x08, Bytes.toBytes("wCellNum8"),Bytes.toBytes(a8[4]));
		put.addColumn(b_0x08, Bytes.toBytes("wStart_Cell_Id8"),Bytes.toBytes(a8[5]));
		put.addColumn(b_0x08, Bytes.toBytes("bCurrent_Cell_Num8"),Bytes.toBytes(a8[6]));

		put.addColumn(b_0x09, Bytes.toBytes("bEnergyStorageSubsystemsNum9"),Bytes.toBytes(a9[0]));
		put.addColumn(b_0x09, Bytes.toBytes("bEnergyStorageSubsystemsNo9"),Bytes.toBytes(a9[1]));
		put.addColumn(b_0x09, Bytes.toBytes("wTemperature_Probes_Num9"),Bytes.toBytes(a9[2]));
		
		putList.add(put);
		long s = System.currentTimeMillis();
		// 将数据集合插入到数据库
		table.put(putList);
		long e = System.currentTimeMillis();
		System.out.println(e-s);
		num++;
	}
	
	public static void close() {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection = null;
	}
}
